package com.vil.config;

import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewFilter;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

//Druid数据源 配置类
@Configuration
public class DruidConfig {
	
	//使其它数据源配置有效
	@ConfigurationProperties(prefix="spring.datasource")
	@Bean(destroyMethod="close",initMethod="init")
	public DataSource druidDataSource(){
		return new DruidDataSource();
	}
	
	//配置监控服务器
	@Bean
	public ServletRegistrationBean<Servlet> statViewServlet(){
		ServletRegistrationBean<Servlet> srb=new ServletRegistrationBean<Servlet>(new StatViewServlet(),"/druid/*");
		//添加白名单
		srb.addInitParameter("allow","");
		//添加黑名单
		srb.addInitParameter("deny","192.168.1.88");
		//添加控制台的用户名和密码
		srb.addInitParameter("loginUsername","admin");
		srb.addInitParameter("loginPassword","999999");
		//是否能重置数据
		srb.addInitParameter("resetEnable","false"); 
		return srb;
	}
	
	//配置监控过滤器
	@Bean
	public FilterRegistrationBean<Filter> statViewFilter(){
		FilterRegistrationBean<Filter> frb=new FilterRegistrationBean<Filter>(new WebStatFilter());
		  frb.addUrlPatterns("/*"); //添加过滤规则
		  //忽略过滤格式
		  frb.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		return frb;
	}
}
